/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.*;

import com.xy.biz.ts.domain.bo.ExaminationQuestionOptionBO;
import com.xy.biz.ts.domain.converter.ExaminationQuestionOptionConverter;
import com.xy.biz.ts.domain.enums.ExaminationQuestionDifficultyEnum;
import com.xy.biz.ts.domain.enums.ExaminationQuestionTypeEnum;
import com.xy.biz.ts.domain.enums.TrainNodeEnum;
import com.xy.biz.ts.domain.req.*;
import com.xy.biz.ts.domain.req.ListExaminationQuestionRequest;
import com.xy.biz.ts.domain.req.UpdateExaminationQuestionRequest;
import com.xy.biz.ts.domain.resp.GetSingleExaminationQuestionResponse;
import com.xy.lang.domain.KeyValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.ts.domain.resp.GetExaminationQuestionResponse;
import com.xy.biz.ts.domain.req.examinationQuestion.GetExaminationQuestionRequest;
import com.xy.biz.ts.domain.query.ExaminationQuestionQuery;
import com.xy.biz.ts.domain.converter.ExaminationQuestionConverter;
import com.xy.biz.ts.client.ExaminationQuestionClient;
import com.xy.biz.ts.service.ExaminationQuestionService;

/**
 *
 *
 * @author wcs
 * @date 2021-3-8
 */
@Api(tags = "培训流程服务-题库")
@Slf4j
@RequestMapping("/api/ts/examination_question")
@RestController
public class ExaminationQuestionController implements ExaminationQuestionClient {

	@Autowired
	private ExaminationQuestionService examinationQuestionService;

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveExaminationQuestion(@RequestBody SaveExaminationQuestionRequest request) {
		examinationQuestionService.saveExaminationQuestion(ExaminationQuestionConverter.toBO(request));
		return Result.success();
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllExaminationQuestion(@RequestBody Collection<SaveExaminationQuestionRequest> collection) {
		examinationQuestionService.saveAllExaminationQuestion(ExaminationQuestionConverter.toBO(collection));
		return Result.success();
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateExaminationQuestion(@RequestBody UpdateExaminationQuestionRequest request) {
		examinationQuestionService.updateExaminationQuestion(ExaminationQuestionConverter.toBO(request));
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteExaminationQuestion(@RequestBody GetExaminationQuestionRequest request) {
		examinationQuestionService.deleteExaminationQuestion(request.getExaminationQuestionId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetSingleExaminationQuestionResponse> getExaminationQuestion(@RequestBody GetExaminationQuestionRequest request) {
		return Result.success(ExaminationQuestionConverter.toResponse(examinationQuestionService.getExaminationQuestion(request.getExaminationQuestionId())));
	}

	@PostMapping(value = "/list")
	public Result<List<GetExaminationQuestionResponse>> listExaminationQuestion(@RequestBody ListExaminationQuestionRequest request) {
		return Result.success(ExaminationQuestionConverter.toResponse(examinationQuestionService.listExaminationQuestion(ExaminationQuestionConverter.toQuery(request))));
	}

	@ApiOperation("统计个数")
	@PostMapping(value = "/count")
	public Result<Integer> count(@RequestBody CountExaminationPaperRuleRequest request) {
		return Result.success(examinationQuestionService.countExaminationQuestion(ExaminationQuestionConverter.toQuery(request)));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetExaminationQuestionResponse>> listExaminationQuestionById(@RequestBody Collection<Long> collection) {
		return Result.success(ExaminationQuestionConverter.toResponse(examinationQuestionService.listExaminationQuestionById(collection)));
	}

	@Override
	public Result<Page<GetExaminationQuestionResponse>> pageExaminationQuestion(com.xy.biz.ts.domain.req.examinationQuestion.ListExaminationQuestionRequest request) {
		return null;
	}

	@PostMapping(value = "/page")
	public Result<Page<GetExaminationQuestionResponse>> pageExaminationQuestion(@RequestBody ListExaminationQuestionRequest request) {
		ExaminationQuestionQuery query = ExaminationQuestionConverter.toQuery(request);
		List<GetExaminationQuestionResponse> list = ExaminationQuestionConverter.toResponse(examinationQuestionService.listExaminationQuestion(query));
		int count = examinationQuestionService.countExaminationQuestion(query);
		return Result.success(new Page<>(list, count));
	}

	//获取试卷试题
	@PostMapping(value = "/page_by_paper_id")
	public Result<List<GetExaminationQuestionResponse>> pageExaminationQuestionByPaperId(@RequestBody GetExaminationPaperRequest request) {
		return examinationQuestionService.pageExaminationQuestionByPaperId(request.getExaminationPaperId());
	}

	@ApiOperation("获取培训节点信息")
	@GetMapping(value = "/list_pre_options")
	public Result<Map<String, List>> listPreOptions() {
		Map<String, List> map = new HashMap<>(4);
		map.put("examinationQuestionType", listStatus());
		map.put("examinationQuestionDifficulty", listStatus1());
		return Result.success(map);
	}

	private List<KeyValue<Integer, String>> listStatus() {
		ExaminationQuestionTypeEnum[] enums = ExaminationQuestionTypeEnum.values();
		List<KeyValue<Integer, String>> list = new ArrayList<>();
		for (ExaminationQuestionTypeEnum e : enums) {
			list.add(e.toKeyValue());
		}
		return list;
	}
	private List<KeyValue<Integer, String>> listStatus1() {
		ExaminationQuestionDifficultyEnum[] enums = ExaminationQuestionDifficultyEnum.values();
		List<KeyValue<Integer, String>> list = new ArrayList<>();
		for (ExaminationQuestionDifficultyEnum e : enums) {
			list.add(e.toKeyValue());
		}
		return list;
	}
}
